package com.ruoyi.project.system.info.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.DataException;
import com.ruoyi.project.system.function.domain.OverviewDB;
import com.ruoyi.project.system.function.service.FunctionService;
import com.ruoyi.project.system.info.domain.UIData;
import com.ruoyi.project.system.info.domain.UIDataPre;
import com.ruoyi.project.system.info.domain.UIUserInfo;
import com.ruoyi.project.system.info.service.IBaseFamilyMemberInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.info.domain.BaseFamilyInfoManagement;
import com.ruoyi.project.system.info.service.IBaseFamilyInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 【农户收入信息管理模块】Controller
 *
 * @author lsx --> chen
 * @date 2023-02-17 --> 2023-06-03
 */
@Controller
//@RequiresRoles(value={"common", "admin"},logical= Logical.OR)
@RequestMapping("/system/info")
@Slf4j
public class BaseFamilyInfoManagementController extends BaseController {
    private String prefix = "system/info";

    @Autowired
    private IBaseFamilyInfoService baseFamilyInfoService;
    @Autowired
    private IBaseFamilyMemberInfoService baseFamilyMemberInfoService;

    @Autowired
    private FunctionService functionService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 返回所有农户收入信息的字段（`base_family_info`表的字段）
     */
//    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseFamilyInfoManagement baseFamilyInfo) {
        startPage();
        List<BaseFamilyInfoManagement> list = baseFamilyInfoService.selectBaseFamilyInfoList(baseFamilyInfo);
        return getDataTable(list);
    }

    /**
     * 修改信息界面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        BaseFamilyInfoManagement baseFamilyInfo = baseFamilyInfoService.selectBaseFamilyInfo(id);
        mmap.put("baseFamilyInfo", baseFamilyInfo);
        return prefix + "/edit";
    }

    /**
     * 查看信息页面
     */
    @GetMapping("/check/{id}")
    public String check(@PathVariable("id") Integer id, ModelMap mmp) {
        BaseFamilyInfoManagement baseFamilyInfo = baseFamilyInfoService.selectBaseFamilyInfo(id);
        mmp.put("baseFamilyInfo", baseFamilyInfo);
        return prefix + "/check";
    }

    /**
     * 农户收入信息的录入提交按钮
     */
    @PostMapping("/dataEntry")
    @ResponseBody
    public AjaxResult dataEntry(@RequestBody String data) {

        UIDataPre dataPre = JSON.parseObject(data, UIDataPre.class);
//        不需要写业务逻辑判断数据是否存在，需要的是写sql语句
        // 获取户编号
        String fid = dataPre.getUserInfo().getUserId();

        // 对应`busi_income_data`表的一条字段（户编号和年份确定出唯一一条）
        OverviewDB overviewDB = functionService.selectOverviewDB(fid, Integer.parseInt(dataPre.getUserInfo().getYear()));
        /**
         * @description:
         * submitInfo是收入的相关数据
         * overviewDB
         * 第三个参数：expense --- paoe 生产经营支出
         * @author chen0
         * @date 2023/6/7 12:18
         * @version 1.0
         */
        baseFamilyInfoService.dataEntry(dataPre.getSubmitInfo(), overviewDB, dataPre.getPaoe());
        return AjaxResult.success();
    }

    /**
     * @description: 在查看或者编辑按钮点击后发送此请求
     * 根据户编号和年份查询相关数据传递给前端
     *    //查询相关农户的基本信息baseInfo
     *    //查询农户收入信息detailInfo
     * @author chen0
     * @date 2023/6/7 13:07
     * @version 1.0
     */
    @GetMapping("/echo{fid}{year}")
    @ResponseBody
    public AjaxResult echoData(@Param("fid") String fid, @Param("year") String year) {
        Map<String, Object> map = baseFamilyInfoService.echo(fid, Integer.parseInt(year));
        return AjaxResult.success(map);
    }
}
